//
//  IndexModel.swift
//
//  Created by eme on 2017/4/10
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol
import UtilCore
import SwiftDate

class IndexModel: ModelProtocol,CCellFlowLayoutProtocol {
    
    /// section的Right距离
    var cellRight: CGFloat = 0.0
    
    /// section的Left距离
    var cellLeft: CGFloat = 0.0
    
    /// section的Bottom距离
    var cellBottom: CGFloat = 0.0
    
    /// section的top距离
    var cellTop: CGFloat = 0.0
    
    /// 每行内部item之间的间距
    var cellminimumInteritemSpacing: CGFloat = 0.0
    
    /// 每行之间的间距
    var cellminimumLineSpacing: CGFloat = 0.0
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kIndexListsizeKey: String = "listsize"
    internal let kIndexHeaderwidthKey: String = "headerwidth"
    internal let kIndexWidthKey: String = "width"
    internal let kIndexT3ListKey: String = "t3_list"
    internal let kIndexT4ListKey: String = "t4_list"
    internal let kIndexT1ListKey: String = "t1_list"
    internal let kIndexHeightKey: String = "height"
    internal let kIndexHeaderimagekeyKey: String = "headerimagekey"
    internal let kIndexT2ListKey: String = "t2_list"
    internal let kIndexHeaderurlKey: String = "headerurl"
    internal let kIndexHeaderheightKey: String = "headerheight"
    internal let kIndexTtypeKey: String = "ttype"
    internal let kIndexTimeKey: String = "time"
    
    
    // MARK: 属性
    var listsize: String
    var headerwidth: Float
    var width: Float
    var t3List: [T3ListModel]
    var t4List: [T4ListModel]
    var t1List: [T1ListModel]
    var height: Float
    var headerimagekey: String
    var t2List: [T2ListModel]
    var headerurl: String
    var headerheight: Float
    var ttype: String{
        didSet{
            switch self.ttype {
            case "t3_2":
                self.cellRight = 0
                self.cellLeft = 0
                self.cellBottom = 10
                self.cellTop = 0
                self.cellminimumInteritemSpacing = 11
                self.cellminimumLineSpacing = 10
            default:
                self.cellRight = 0
                self.cellLeft = 0
                self.cellBottom = 0
                self.cellTop = 0
            }
        }
    }
    var time: String
    //0 表示显示图片 1表示不显示
    var isDefault:Int
    ///如果不显示图片的时候需要显示文字标题
    var title_size:Int
    var title_color:String
    var title:String
    var bg_color:String
    var pic:String
    var bigheight:Float
    
    var endtime:String
    var begintime:String
    var curtime:String
    ///筛选标志
    var filterFlag:Bool{
        get{
            /// 只有秒杀才会判断 有效时间的
            if self.ttype == "t4_1" {
                do{
                    let begin = try self.begintime.date(format: DateFormat.custom("yyyy-MM-dd HH:mm"), fromRegion: nil).absoluteDate
                    let end = try self.endtime.date(format: DateFormat.custom("yyyy-MM-dd HH:mm"), fromRegion: nil).absoluteDate
                    let cur = try self.curtime.date(format: DateFormat.custom("yyyy-MM-dd HH:mm"), fromRegion: nil).absoluteDate
                    if cur > begin && cur < end {
                        return true
                    }else{
                        return false
                    }
                } catch {
                    return false
                }
            }else{
                return true
            }
        }
    }
    /**
     1.这块需要说明下，以下属性使用与t1_4,t1_5用的
     img_Spacing:图片的边距
     img_InteritemSpacing:两个image 之间的距离
     */
    
    /// 图片的左右边距
    var img_Left_Spacing:CGFloat = 8.0
    /// 图片的上下边距
    var img_Top_Spacing:CGFloat = 5.0
    /// 两个image 之间的距离
    var img_InteritemSpacing:CGFloat = 9.0
    
    ///header的高度更具宽度计算得出
    var h_height:CGFloat{
        get{
            if self.headerwidth > 0 {
                return self.h_width*CGFloat(self.headerheight/self.headerwidth)
            }else{
                return 0
            }
            
        }
    }
    ///header的宽度为屏幕的宽度
    var h_width:CGFloat{
        get{
            return screen_Width
        }
    }
    ///item的高度更具宽度计算得出
    var i_height:CGFloat{
        get{
            if self.width > 0 {
                return CGFloat(self.height)
            }else{
                /// 默认给95的高度
                return 510
            }
            
        }
    }
    /// item的宽度为屏幕的宽度
    var i_width:CGFloat{
        get{
            return screen_Width
        }
    }
    /*
     1.每个item的数量，一般图文类和商品横向滑动的为1其余其它都是返回数组的count
     2.显示规则为:
     {
     *.t2_1 t1_1,t1_2,t1_3,t1_4,t1_5,t1_6,t4_1,t4_2:return 1
     *.t3_1,t3_2:return t3_list.count
     *.t4_3,t4_4,t4_5,t4_6:return t4_list.count
     
     }
     */
    var numberOfItemsInSection:Int{
        get{
            //            ///这块是开发的时候
            //            switch self.ttype {
            //            case "t1_1","t1_2","t1_3","t1_5","t1_4","t2_1","t3_1","t4_1","t4_2":
            //                return 1
            //            case "t3_2":
            //                return self.t3List.count
            //            case "t4_3","t4_4":
            //                return self.t4List.count
            //            case "t4_5":
            //                return self.t4List.count
            //            default:
            //                return 0
            //            }
            switch self.ttype {
            case "t3_2":
                return self.t3List.count
            case "t4_3","t4_4","t4_5","t4_6":
                return self.t4List.count
            default:
                return 1
            }
        }
    }
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    public required  init?(json: JSON) {
        listsize = json[kIndexListsizeKey].stringValue
        headerwidth = json[kIndexHeaderwidthKey].floatValue
        width = json[kIndexWidthKey].floatValue
        t3List = []
        if let items = json[kIndexT3ListKey].array {
            for item in items {
                if let t3ListModel = T3ListModel(json: item){
                    t3List.append(t3ListModel)
                    
                }
            }
        }
        t4List = []
        if let items = json[kIndexT4ListKey].array {
            for item in items {
                if let t4ListModel = T4ListModel(json: item){
                    t4List.append(t4ListModel)
                }
            }
        }
        t1List = []
        if let items = json[kIndexT1ListKey].array {
            for item in items {
                if let t1ListModel = T1ListModel(json: item){
                    t1List.append(t1ListModel)
                }
            }
        }
        height = json[kIndexHeightKey].floatValue
        headerimagekey = json[kIndexHeaderimagekeyKey].stringValue
        t2List = []
        if let items = json[kIndexT2ListKey].array {
            for item in items {
                if let t2ListModel = T2ListModel(json: item){
                    t2List.append(t2ListModel)
                }
            }
        }
        headerurl = json[kIndexHeaderurlKey].stringValue
        headerheight = json[kIndexHeaderheightKey].floatValue
        ttype = json[kIndexTtypeKey].stringValue
        time = json[kIndexTimeKey].stringValue
        isDefault = json["isDefault"].intValue
        /// 默认除以2
        title_size = json["title_size"].intValue/2
        let titleColor = json["title_color"].stringValue
        if let from =  titleColor.range(of: "#")?.upperBound {
            title_color = titleColor.substring(from: from)
        }else{
            title_color = "000000"
        }
        title = json["title"].stringValue
        let bgColor = json["background_color"].stringValue
        if let from =  bgColor.range(of: "#")?.upperBound {
            bg_color = bgColor.substring(from: from)
        }else{
            bg_color = "f8f8f8"
        }
        pic = json["pic"].stringValue
        bigheight = json["bigheight"].floatValue
        endtime = json["endtime"].stringValue
        begintime = json["begintime"].stringValue
        curtime = json["curtime"].stringValue
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(listsize as AnyObject, forKey: kIndexListsizeKey)
        
        
        dictionary.updateValue(headerwidth as AnyObject, forKey: kIndexHeaderwidthKey)
        
        
        dictionary.updateValue(width as AnyObject, forKey: kIndexWidthKey)
        
        var temp: [AnyObject] = []
        for item in t3List {
            temp.append(item.dictionaryRepresentation() as AnyObject)
        }
        dictionary.updateValue(temp as AnyObject, forKey: kIndexT3ListKey)
        
        var temp1: [AnyObject] = []
        for item in t4List {
            temp1.append(item.dictionaryRepresentation() as AnyObject)
        }
        dictionary.updateValue(temp1 as AnyObject, forKey: kIndexT4ListKey)
        
        var temp2: [AnyObject] = []
        for item in t1List {
            temp2.append(item.dictionaryRepresentation() as AnyObject)
        }
        dictionary.updateValue(temp2 as AnyObject, forKey: kIndexT1ListKey)
        
        
        dictionary.updateValue(height as AnyObject, forKey: kIndexHeightKey)
        
        
        dictionary.updateValue(headerimagekey as AnyObject, forKey: kIndexHeaderimagekeyKey)
        
        var temp3: [AnyObject] = []
        for item in t2List {
            temp3.append(item.dictionaryRepresentation() as AnyObject)
        }
        dictionary.updateValue(temp3 as AnyObject, forKey: kIndexT2ListKey)
        
        
        dictionary.updateValue(headerurl as AnyObject, forKey: kIndexHeaderurlKey)
        
        
        dictionary.updateValue(headerheight as AnyObject, forKey: kIndexHeaderheightKey)
        
        
        dictionary.updateValue(ttype as AnyObject, forKey: kIndexTtypeKey)
        
        
        dictionary.updateValue(time as AnyObject, forKey: kIndexTimeKey)
        
        
        return dictionary
    }
    
    
}
extension   IndexModel{
    /// 设置每行内部item之间的间距
    ///
    /// - Returns: 返回每行内部item之间的间距
    func get_minimumInteritemSpacing( ) -> CGFloat{
        switch self.ttype {
        case "t4_3":
            return T4_3_CCell.interitemSpacing
        case "t4_4":
            return T4_4_CCell.interitemSpacing
        default:
            return 0
        }
    }
    
    /// 设置每行之间的间距
    ///
    /// - Returns: 返回每行之间的间距
    func get_minimumLineSpacing() -> CGFloat{
        switch self.ttype {
        case "t3_2":
            return 11
        case "t4_3":
            return T4_3_CCell.lineSpacing
        case "t4_4":
            return T4_4_CCell.lineSpacing
        default:
            return 0
        }
    }
    
    /// 设置每个item的size
    ///
    /// - Returns:返回每个item的size
    func get_cellItemSize( ) -> CGSize{
        switch self.ttype {
        case "t1_1","t1_2","t1_6","t1_6":
            return CGSize(width: screen_Width, height: screen_Width * CGFloat(self.height/750))
        case "t1_3":
            return CGSize(width: screen_Width, height: screen_Width * CGFloat(self.bigheight/750) + self.img_Top_Spacing + self.img_Top_Spacing)
        case "t1_4","t1_5":
            return CGSize(width: screen_Width, height: screen_Width * CGFloat(self.height/750) + self.img_Top_Spacing + self.img_Top_Spacing)
        case "t3_1":
            let lineNum = (self.t3List.count + 3)/4
            let height = T3_1_CCell.cellSize.height * CGFloat(lineNum) + T3_1_CCell.cellTop + T3_1_CCell.cellBottom
            return CGSize(width: screen_Width, height: height)
        case "t3_2":
            let width = (screen_Width - self.get_minimumLineSpacing())/2
            let height = width * 182/364
            return CGSize(width: width, height: height)
        case "t2_1":
            ///表示平台分类
            let lineNum = (self.t2List.count + 3)/4
            return CGSize(width: screen_Width, height: T2_1_CCell.cellSize.height * CGFloat(lineNum) + T2_1_CCell.cellTop + T2_1_CCell.cellBottom)
        case "t4_1","t4_2":
            let width = screen_Width
            //            let height = width * 442/750
            return CGSize(width: width, height: ceil(438 / 2))
        case "t4_3":
            return T4_3_CCell.cellSize
        case "t4_4":
            return T4_4_CCell.cellSize
        case "t4_5":
            //            return CGSize(width: screen_Width, height: (screen_Width * 226)/750)
            return CGSize(width: screen_Width, height: 226/2)
        case "t4_6":
            return CGSize(width: screen_Width, height: screen_Width - 10 - 10 + 10 + 8 + 21 + 7 + 19 + 10)
        default:
            return CGSize.zero
        }
    }
    /// 设置section 的边距
    ///
    /// - Returns: 返回每个section 的边距
    func get_cellEdgeInsets( ) -> UIEdgeInsets{
        switch self.ttype {
        case "t3_2":
            return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        case "t4_3":
            return T4_3_CCell.cellEdgeInsets
        case "t4_4":
            return T4_4_CCell.cellEdgeInsets
        case "t4_5":
            return UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
        default:
            return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        
    }
    /// 设置每个section的header的size
    ///
    /// - Returns:返回每个section的header的size
    func get_cellSectionHeaderSize( ) -> CGSize{
        switch self.ttype {
        case "t3_1","t3_2","t4_1","t4_2","t4_3","t4_4","t4_5","t4_6":
            return CGSize(width: screen_Width, height: screen_Width * CGFloat(self.headerheight/750))
        default:
            return CGSize.zero
        }
    }
    /// 设置每个section的Footer的size
    ///
    /// - Returns:返回每个section的Footer的size
    func get_cellSectionFooterSize( ) -> CGSize{
        return CGSize.zero
    }
}
